(function() {
    "use strict";
   /* $rootScope.confirm = function(url,msg,fn){
        var config = {btn: ['ok','cancel'],title:false};
        layer.confirm(msg,config,function(index){
            $http.get(url).success(function(data){
                fn&& fn(data);
                layer.close(index);
            });
        });
    }*/
    window.confirm = function(message,fn){
        layer.confirm(message, {btn: ['确定','取消'],title:false },function(index){ fn && fn();  layer.close(index);},function(){});
    }
    window.msg = function(tip){layer.msg(tip, {time: 2000, icon:1});}
    window.alert = function(tip){layer.msg(tip, {time: 2000, icon:5});}
    window.tips = function(tip,dom){var tipId = layer.tips(tip,dom,{ tips: [1,'#ff784e']}); $(dom).attr("tipId",tipId) };
    window.closeTip = function(dom){layer.close($(dom).attr("tipId")) };
    window.popOver = function(tip,dom){ layer.tips(tip,dom,{ tips: [1,'#444']}) };

    var app = angular.module("com.feng", []);


    app.factory("dataService", function($http) {

        return {
            // 本地导入
            upload: function(options,fn) {
                if(typeof(options.params)!="object"){
                    options.params = {};
                }
                if(!options.url){
                    options.url = '../common/uploadImg';
                }
                var index;
               if(options.nowait){
                   options.onSubmit = function(){};
               }else{
                   options.onSubmit = function(){
                       index = layer.load(1, {
                           shade: [0.2,'#000'] //0.1透明度的白色背景
                       });
                   };
               }
                options.onComplate = function(data) {
                    layer.close(index);
                    if(Array.isArray(data)){
                        fn&&fn(data);
                    }else if("object" == typeof data){
                        if(data.success){
                            fn&&fn(data);
                        }else{
                            layer.alert(data.error||"上传发生错误!",{btn: ['ok'],title:false});
                        }
                    }else{
                        layer.alert("发生错误:"+data,{btn: ['ok'],title:false});
                    }
                };
                // 上传方法
                $.upload(options);
            }
        }});

    app.controller('stevenCtl', function($scope,$http,$interval,dataService) {

        $http.post("/steven/getBills").success(function(list){
            $scope.list = list;
        });
        $scope.uploadExcel= function(){
            dataService.upload({url:"/steven/uploadExcel",params:{},
                accept:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"},function(resp){
                if(resp.success){
                    $scope.list = resp.list;
                    $scope.$apply();
                }else{
                    alert(data.error);
                }
            })
        }

        $scope.compareExcel= function(){
            if(!$scope.list ||$scope.list.length==0){
                alert("请选上传台账!");
                return;
            }
            dataService.upload({url:"/steven/compareExcel",params:{},nowait:true,
                accept:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"},function(resp){
            })
        }


    });


})();